import { observable } from 'mobx'
import Taro from '@tarojs/taro'
import { loginApi } from './api'

class Store {
  @observable userInfo = {}
  @observable loading = true

  init = () => {
    this.getUserInfo()
  }

  getUserInfo = async () => {
    const codeInfo = await Taro.login()
    const code = codeInfo.code
    
    const params = {
      code,
    }
    this.loading = true
    const { data = {}, success } = await loginApi(params)
    this.loading = false
    console.log("getUserInfo:", data)
    if(success) {
      this.userInfo = data
      Taro.setStorageSync("salt", data.salt)
    }
  }

  setUserInfo = (data) => {
    this.userInfo = data
    this.loading = false
  }
}

export default new Store()